﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PaintDotNet;

namespace Paint.NET_CRAW
{
    public class CrawType : FileType
    {
        public CrawType()
            : base("Calc# Raw image format",
                FileTypeFlags.SupportsLoading | FileTypeFlags.SupportsSaving,
                new String[] { ".craw" })
        {
        }

        protected override Document OnLoad(Stream input)
        {
            try
            {
                MessageBox.Show(this.DefaultExtension);
                RawDriver drv = new RawDriver(input);
                Bitmap b = drv.ReadBitmap();
                return Document.FromImage(b);
            }
            catch
            {
                MessageBox.Show("Problem Importing File");
                Bitmap b = new Bitmap(500, 500);
                return Document.FromImage(b);
            }
        }

        protected override void OnSave(Document input, Stream output, SaveConfigToken token,
            Surface scratchSurface, ProgressEventHandler callback)
        {
            RenderArgs ra = new RenderArgs(new Surface(input.Size));
            input.Render(ra);
            SaveOptions sv = new SaveOptions();
            if (sv.ShowDialog() == DialogResult.OK)
            {
                RawDriver drv = new RawDriver(output);
                drv.WriteBitmap(ra.Bitmap, sv.type);
            }
        }
    }

    public class MyFileTypeFactory : IFileTypeFactory
    {
        public FileType[] GetFileTypeInstances()
        {
            return new FileType[] { new CrawType() };
        }
    }
}
